﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace lchealthy
{
    public partial class frmNewDietitian : Form
    {   DataSet ds;
        int dID=-1;
        public frmNewDietitian()
        {
            InitializeComponent();
        }

        public frmNewDietitian(int id)
        {
            dID = id;
            InitializeComponent();
        }
        private void frmNewDietitian_Load(object sender, EventArgs e)
        {
            string sql;
            if (dID != -1)
            {
                sql = "select * from dietitianinfo where dID=" + dID;
                ds=DbHelperOleDb.Query(sql);
                txtName.Text=ds.Tables[0].Rows[0]["dName"].ToString();
                txtPwd.Text = ds.Tables[0].Rows[0]["dPwd"].ToString();
                txtRePwd.Text = ds.Tables[0].Rows[0]["dPwd"].ToString();
                txtTel.Text = ds.Tables[0].Rows[0]["dTel"].ToString();
                txtAddress.Text = ds.Tables[0].Rows[0]["dAddress"].ToString();
                txtQQ.Text = ds.Tables[0].Rows[0]["dQQ"].ToString();
            }

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string name = txtName.Text;
            string pwd = txtPwd.Text;
            string rePwd = txtRePwd.Text;
            string tel = txtTel.Text;
            string address = txtAddress.Text;
            string qq = txtQQ.Text;

            string sql;
            int i;
            if (txtName.Text == null)
            {
                MessageBox.Show("请输入营养师的姓名！", "提示", MessageBoxButtons.YesNo);
            }
            else
            {

                if (txtPwd.Text == txtRePwd.Text)
                {
                    if (dID == -1)
                    {
                        sql = string.Format(@"insert into dietitianinfo (dName,dPwd,dTel,dAddress,dQQ) values('{0}','{1}','{2}','{3}','{4}')", name, pwd, tel, address, qq);
                        i = DbHelperOleDb.ExecuteSql(sql);
                        if (i == 1)
                        {
                            MessageBox.Show("添加营养师成功！");
                        }
                        else
                        {
                            MessageBox.Show("添加营养师失败！");
                        }
                    }
                    else 
                    {
                        sql = string.Format(@"update dietitianinfo set dName='{0}',dPwd='{1}',dTel='{2}',dAddress='{3}',dQQ='{4}' where dID={5}",name,pwd,tel,address,qq,dID );
                        i = DbHelperOleDb.ExecuteSql(sql);
                        if (i == 1)
                        {
                            MessageBox.Show("修改营养师信息成功！");
                        }
                        else
                        {
                            MessageBox.Show("修改营养师信息失败！");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("请输入的确认密码和密码相同！", "提示", MessageBoxButtons.YesNo);
                }
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtName.Text = "";
            txtPwd.Text = "";
            txtRePwd.Text = "";
            txtTel.Text = "";
            txtAddress.Text = "";
            txtQQ.Text = "";
        }

       

        
       
    }
}
